package dz.chuxin.app.sample;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;

import dz.chuxin.app.core.BaseRecyclerActivity;
import dz.chuxin.framework.widget.recycler.BaseViewHolder;
import dz.chuxin.framework.widget.recycler.ChuXinRecyclerView;
import dz.chuxin.framework.widget.recycler.ILayoutManager;
import dz.chuxin.framework.widget.recycler.MyStaggeredGridLayoutManager;

/**
 * Created by chuxin on 2016/7/1.
 */
public class RecyclerViewSampleActivity extends BaseRecyclerActivity<String> {

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        super.setUpData(savedInstanceState);
        mRecyclerView.setRefreshingOn();
    }

    @Override
    protected void setUpToolbarComplete(Toolbar toolbar) {
        mToolbar.setTitle("列表测试");
    }

    @Override
    protected BaseViewHolder getRecyclerViewHolder(ViewGroup parent, int viewType) {
        return new SampleViewHolder(LayoutInflater.from(this).inflate(android.R.layout.simple_list_item_1, null));
    }

    @Override
    protected ILayoutManager getLayoutManager() {
        return new MyStaggeredGridLayoutManager(3, LinearLayout.VERTICAL);
    }

    @Override
    public void onRefresh(int action) {
        if (action == ChuXinRecyclerView.ACTION_PULL_TO_REFRESH) {
            mCurrentPage = 1;
            mDataList.clear();
        }
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < 20; i++) {
                    mDataList.add("RecyclerView Item " + i);
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                mRecyclerView.onRefreshComplete();
                mRecyclerAdapter.notifyDataSetChanged();
            }
        }.execute();
    }

    // ----
    class SampleViewHolder extends BaseViewHolder {
        TextView mTextView;

        public SampleViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(android.R.id.text1);
        }

        @Override
        protected void onBindViewHolder(int position) {
            mTextView.setText(mDataList.get(position));
        }
    }
}
